package com.campus.activity.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.campus.activity.entity.Feedback;
import com.campus.activity.mapper.FeedbackMapper;
import com.campus.activity.service.FeedbackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 反馈评价服务实现类
 * 
 * @author System
 * @since 2024
 */
@Service
public class FeedbackServiceImpl extends ServiceImpl<FeedbackMapper, Feedback> implements FeedbackService {

    @Autowired
    private FeedbackMapper feedbackMapper;

    @Override
    public boolean addFeedback(Feedback feedback) {
        // 检查是否已经反馈过
        if (hasStudentFeedback(feedback.getStudentId(), feedback.getActivityId())) {
            return false;
        }
        
        feedback.setCreateTime(LocalDateTime.now());
        feedback.setUpdateTime(LocalDateTime.now());
        feedback.setDeleted(0);
        return save(feedback);
    }

    @Override
    public boolean replyFeedback(Integer feedbackId, String replyContent) {
        Feedback feedback = new Feedback();
        feedback.setFeedbackId(feedbackId);
        feedback.setReplyContent(replyContent);
        feedback.setReplyTime(LocalDateTime.now());
        feedback.setUpdateTime(LocalDateTime.now());
        return updateById(feedback);
    }

    @Override
    public List<Feedback> getFeedbackInfo(Integer activityId) {
        return feedbackMapper.selectByActivityId(activityId);
    }

    @Override
    public List<Feedback> getFeedbackByStudentId(Integer studentId) {
        return feedbackMapper.selectByStudentId(studentId);
    }

    @Override
    public Map<String, Object> getActivitySatisfactionStats(Integer activityId) {
        Map<String, Object> stats = new HashMap<>();
        
        List<Feedback> feedbacks = getFeedbackInfo(activityId);
        
        if (feedbacks.isEmpty()) {
            stats.put("totalFeedbacks", 0);
            stats.put("averageRating", 0.0);
            stats.put("ratingDistribution", new HashMap<>());
            return stats;
        }
        
        double totalRating = 0;
        Map<Integer, Integer> ratingDistribution = new HashMap<>();
        
        for (Feedback feedback : feedbacks) {
            Integer rating = feedback.getSatisfactionScore();
            totalRating += rating;
            ratingDistribution.put(rating, ratingDistribution.getOrDefault(rating, 0) + 1);
        }
        
        stats.put("totalFeedbacks", feedbacks.size());
        stats.put("averageRating", totalRating / feedbacks.size());
        stats.put("ratingDistribution", ratingDistribution);
        
        return stats;
    }

    @Override
    public List<Feedback> getUnrepliedFeedback(Integer activityId) {
        return feedbackMapper.selectUnrepliedByActivityId(activityId);
    }

    @Override
    public List<Feedback> exportFeedbackData(Integer activityId) {
        QueryWrapper<Feedback> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("activity_id", activityId)
                   .eq("deleted", 0)
                   .orderByDesc("create_time");
        return list(queryWrapper);
    }

    @Override
    public boolean hasStudentFeedback(Integer studentId, Integer activityId) {
        Feedback feedback = feedbackMapper.selectByActivityIdAndStudentId(activityId, studentId);
        return feedback != null;
    }
}